package com.bicon.bdata_service_search.es.service.Impl;

import com.bicon.bdata_service_search.es.bean.ResultAskAnswerListBean;
import com.bicon.bdata_service_search.es.entities.QuestionAndAnswer;
import com.bicon.bdata_service_search.es.repository.ElasticQuestionAndAnswerRepository;
import com.bicon.bdata_service_search.es.service.ESQuestionAndAnswerService;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.ElasticsearchClient;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;

import static org.elasticsearch.index.query.QueryBuilders.queryStringQuery;

/**
 * 描述:
 * es question impl
 *
 * @author zhaoyu
 * @email zhaoyu@vv.cc
 * @create 2018-08-23 11:50
 */
@Service("esQuestionAndAnswerServiceImpl")
public class ESQuestionAndAnswerServiceImpl implements ESQuestionAndAnswerService {

    @Autowired
    private ElasticQuestionAndAnswerRepository elasticQuestionAndAnswerRepository;
    @Autowired
    private ElasticsearchClient esClient;

    @Override
    public Page<QuestionAndAnswer> findAll() {
        return elasticQuestionAndAnswerRepository.findAll();
    }

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;
//    @Override
//    public Page<QuestionAndAnswer> findByAsk_content(String ask_content, Pageable pageable) {
//        return elasticQuestionAndAnswerRepository.findByAsk_content(ask_content,pageable);
//    }

    @Override
    public Page<QuestionAndAnswer> findByAskContentIsLike(String ask_content, Pageable page) {
        return elasticQuestionAndAnswerRepository.findByAskContentIsLike(ask_content, page);
    }

    @Override
    public Page<QuestionAndAnswer> findByAskTitleIsLike(String askTitle, Pageable page) {
        return elasticQuestionAndAnswerRepository.findByAskTitleIsLike(askTitle, page);
    }

    @Override
    public Page<QuestionAndAnswer> findByAskContentLike(String askTitle, Pageable pageable) {
        return elasticQuestionAndAnswerRepository.findByAskContentLike(askTitle, pageable);
    }

    @Override
    public Page<QuestionAndAnswer> queryByAskContentLessThan(String askTitle, Pageable pageable) {

//        MultiMatchQuery.QueryBuilder queryBuilder = boolQuery().should(
//                queryString("Mike Mat").analyzeWildcard(true)
//                        .field("firstName", 2.0f).field("lastName").field("title")
//                        .field("location").field("industry").field("email"));
//
//        SortBuilder<>
        SearchQuery searchQuery = new NativeSearchQueryBuilder()
//                .withQuery(matchQuery("askContent", askTitle).slop(1)).withPageable(pageable).withSort()
//                .withQuery(matchQuery("askContent", askTitle).slop(1)).withPageable(pageable)
//                .withQuery(matchPhraseQuery("askContent",askTitle)).withPageable(pageable)
                .withQuery(queryStringQuery("askContent:'"+askTitle))
                .build();

        Page<QuestionAndAnswer> sampleEntities =
//                elasticsearchTemplate.queryForPage(searchQuery, QuestionAndAnswer.class);
                elasticsearchTemplate.queryForPage(searchQuery,QuestionAndAnswer.class);
        return elasticQuestionAndAnswerRepository.queryQuestionAndAnswersByAskContentIn(askTitle,pageable);
//        return sampleEntities;
    }

    @Override
    public Page<QuestionAndAnswer> findByAskContent(String askContent, Pageable pageable) {
        return elasticQuestionAndAnswerRepository.findByAskContent2(askContent,pageable);
    }

    @Override
    public Page<QuestionAndAnswer> esFind1(String askContent, Pageable pageable) {
//        return elasticQuestionAndAnswerRepository.findQuestionAndAnswerByAskContentContains(askContent,pageable);
//        return elasticQuestionAndAnswerRepository.findByAskContentContains(askContent,pageable);
//        return elasticQuestionAndAnswerRepository.findByAskContentIsContaining(askContent,pageable);
        return elasticQuestionAndAnswerRepository.findByAskContent(askContent,pageable);
    }

    @Override
    public Page<QuestionAndAnswer> getByAskContentMd5Is(String askContentMd5, Pageable pageable) {
        return elasticQuestionAndAnswerRepository.findByAskContentMd5(askContentMd5,pageable);
    }

    @Override
    public ResultAskAnswerListBean getResultAskAnswerListBean(Page<QuestionAndAnswer> questionAndAnswers) {
        ResultAskAnswerListBean resultAskAnswerListBean = new ResultAskAnswerListBean();
        resultAskAnswerListBean.setTotal((int) questionAndAnswers.getTotalElements());
        resultAskAnswerListBean.setCurrent(questionAndAnswers.getNumber());
        resultAskAnswerListBean.setSize(questionAndAnswers.getSize());
        return null;
    }

    @Override
    public Page<QuestionAndAnswer> search(String askContent, Pageable pageable) {
        QueryBuilder queryBuilder = QueryBuilders.queryStringQuery("我感冒了，怎么办");
//        QueryBuilders.wildcardQuery(conColumn, "*"+conValue+"*");
//        elasticQuestionAndAnswerRepository.search(queryBuilder,pageable);

        return elasticQuestionAndAnswerRepository.search(queryBuilder,pageable);
    }

//    @Override
//    public QuestionAndAnswer findById(String Id) {
//        return elasticQuestionAndAnswerRepository.findById();
//    }

    @Override
    public Page<QuestionAndAnswer> clientSearch(String askContent, Pageable pageable) {

        SearchResponse res = null;

        QueryBuilder queryBuilder = QueryBuilders.queryStringQuery(askContent);
//        res = esClient
//        esClient

        return null;
    }


}